<?php

/***********************************************************
 * 					   TRANSACTIONS
 ***********************************************************/

/**
 * Begin a DB transaction
 * @author Pablo
 * @since 1.0
 */
function blg_TRANSACTION(){
	global $_DB;
	$query= 'START TRANSACTION;';
	mysql_query($query, $_DB->link_identifier);	
}

/**
 * Commit changes in a DB transaction
 * @author Pablo
 * @since 1.0
 */
function blg_COMMIT(){
	global $_DB;
	$query= 'COMMIT;';
	mysql_query($query, $_DB->link_identifier);		
}

/**
 * Rollback changes in a DB transaction
 * @author Pablo
 * @since 1.0
 */
function blg_ROLLBACK(){
	global $_DB;
	$query= 'ROLLBACK;';
	mysql_query($query, $_DB->link_identifier);		
}

/***********************************************************
 * 					  QUERY DATABASE
 ***********************************************************/	

/**
 * Execute a SELECT query on the DB
 * @param $query
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select($query){
	global $_DB;
	$result= mysql_query($query, $_DB->link_identifier);
	return blg_dbschema_std_result($result, 'select', $query);		
}

/**
 * Execute a INSERT query on the DB
 * @param $query
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert($query){
	global $_DB;
	
	$result= mysql_query($query, $_DB->link_identifier);
	return blg_dbschema_std_result($result, 'insert', $query);
}

/**
 * Execute a UPDATE query on the DB
 * @param $query
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update($query){
	global $_DB;
	$result= mysql_query($query, $_DB->link_identifier);
	echo mysql_error();
	return blg_dbschema_std_result($result, 'update', $query);
}

/**
 * Execute a DELETE query on the DB
 * @param $query
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete($query){
	global $_DB;
	$result= mysql_query($query, $_DB->link_identifier);
	return blg_dbschema_std_result($result, 'delete', $query);
}

/**
 * Standarize queries results
 *
 * @param resource $result
 * @param string $type Choose between [ select | insert | update | delete ] 
 * @param string $query [Optional]
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_std_result($result, $type, $query= ''){
	global $_DB;
		
	$return= new stdClass();	
	
	$return->type= $type;
	if(!empty($query))
		$return->query= $query;	
	
	if(!$result){
		$error= mysql_error();
		blg_log('BLG_MYSQL: '.trim($query).' '.$error);
		$return->success= FALSE;
		$return->error= $error;
		return $return;
	}
	
	if($type=='insert' || $type=='update' || $type=='delete'){		 		
		$return->affected_rows= mysql_affected_rows($_DB->link_identifier);
		$return->success= TRUE;
	}
		
	if($type=='insert'){
		if(($id= mysql_insert_id($_DB->link_identifier))>0)
			$return->new_id= $id;
	}
		
	if($type=='select'){
		$return->success= TRUE;
		$return->num_rows= mysql_num_rows($result);
		$return->num_cols= mysql_num_fields($result);	
	
		$i= 0;
		while ($row = mysql_fetch_assoc($result)) {
			$return->$i= $row;
			$i++;	    
		}
	}
	
	return $return;
}


/***********************************************************
 *						SELECT QUERIES
 ***********************************************************/

/**
 * Select messages
 * 
 * @param array	$options		
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_messages($options= NULL, $fields= NULL){
	$db_prefix = blg_get_config('dbprefix');
	$tbl_prefix= 'msg';
		
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix.`id`= %s", mysql_real_escape_string($options['id']));							
					
		//from_guid
		if(array_key_exists('from_guid', $options))
			$wheres[]= sprintf("$tbl_prefix.`from_guid`= %s", mysql_real_escape_string($options['from_guid']));

		//to_guid
		if(array_key_exists('to_guid', $options))
			$wheres[]= sprintf("$tbl_prefix.`to_guid`= %s", mysql_real_escape_string($options['to_guid']));

		//subject
		if(array_key_exists('subject', $options))
			$wheres[]= sprintf("$tbl_prefix.`subject`= %s", mysql_real_escape_string($options['subject']));
			
		//message
		if(array_key_exists('message', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("$tbl_prefix.`message`= '%s'", mysql_real_escape_string($options['message']));			

		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "$tbl_prefix.`time_created` ".blg_dbschema_query_time_created($options);			

		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "$tbl_prefix.`time_updated` ".blg_dbschema_query_time_updated($options);

		//status
		if(array_key_exists('status', $options))
			$wheres[]= sprintf("$tbl_prefix.`status` LIKE '%s'", mysql_real_escape_string($options['status']));						

		//reply_id
		if(array_key_exists('reply_id', $options))
			$wheres[]= sprintf("$tbl_prefix.`reply_id`= %s", mysql_real_escape_string(empty($options['reply_id'])?'NULL':$options['reply_id']));

			
		//where	
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);		
	}
	
	$query= new stdClass();
		
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}messages {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`id`";
	
	return $query;
}

//TODO: no more suport for files on database
/**
 * Select files
 * 
 * @param array	$options		
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_files($options= NULL, $fields= NULL, $headers_only= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix= 'f';
		
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix.`id`= %s", mysql_real_escape_string($options['id']));				
		
		//url
		if(array_key_exists('url', $options))
			$wheres[]= sprintf("$tbl_prefix.`url`= '%s'", mysql_real_escape_string($options['url']));
			
		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("$tbl_prefix.`name`= '%s'", mysql_real_escape_string($options['name']));
			
		if(array_key_exists('original_name', $options))
			$wheres[]= sprintf("$tbl_prefix.`original_name`= '%s'", mysql_real_escape_string($options['original_name']));			
			
		//extension
		if(array_key_exists('ext', $options))
			$wheres[]= sprintf("$tbl_prefix.`ext`= '%s'", mysql_real_escape_string($options['ext']));			

		//size
		if(array_key_exists('size', $options))
			$wheres[]= sprintf("$tbl_prefix.`size`= '%s'", mysql_real_escape_string($options['size']));
			
		//mime-type
		if(array_key_exists('mime', $options))
			$wheres[]= sprintf("$tbl_prefix.`mime`= '%s'", mysql_real_escape_string($options['mime']));
			
		//owner
		if(array_key_exists('owner', $options))
			$wheres[]= sprintf("$tbl_prefix.`owner`= %s", mysql_real_escape_string($options['owner']));	

		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "$tbl_prefix.`time_created` ".blg_dbschema_query_time_created($options);

		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "$tbl_prefix.`time_updated` ".blg_dbschema_query_time_updated($options);	

		// info
		if(array_key_exists('info', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("$tbl_prefix.`info`= '%s'", mysql_real_escape_string($options['info']));			
			
			
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	
	if($headers_only===TRUE)
		$fields= array('id', 'url', 'name', 'original_name', 'ext', 'mime', 'size', 'time_created', 'time_updated', 'owner', 'info');		

	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);	
	$query->from= 	"{$db_prefix}files {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`id`";
	
	return $query;
}

/**
 * Select configuration row
 * 
 * @param array	$options
 * @param mixed $fields		
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_config($options= NULL, $fields= NULL){
	$db_prefix = blg_get_config('dbprefix');
	$tbl_prefix= 'config';
		
	if(is_array($options)){
		$wheres= array();
				
		//param
		if(array_key_exists('param', $options))
			$wheres[]= sprintf("$tbl_prefix.`param`= '%s'", mysql_real_escape_string($options['param']));			


		//where	
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);		
	}
	
	$query= new stdClass();
		
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}config {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`param`";
	
	return $query;
}

/**
 * Select metastrings
 * 
 * @param array	$options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_metastrings($options= NULL, $fields= NULL){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix= 'ms';
		
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix.`id`= %s", mysql_real_escape_string($options['id']));		
		
		//string
		if(array_key_exists('string', $options))
			$wheres[]= sprintf("$tbl_prefix.`string`= '%s'", mysql_real_escape_string($options['string']));

		//where
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}metastrings {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`id`";
	
	return $query;
}			


/**
 * Select metadata
 * 
 * @param array	$options		
 * @param bool 	$strict_enabled	If true (default), returns only enabled metadata
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_metadata($options= NULL, $fieldsMetaData= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_md= 'md';
	$tbl_prefix_ms1= 'ms1';
	$tbl_prefix_ms2= 'ms2';
		
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`id`= %s", mysql_real_escape_string($options['id']));				
		
		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`entity_guid`= %s", mysql_real_escape_string($options['entity_guid']));
			
		//name (as a string)
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("$tbl_prefix_ms1.`string`= '%s'", mysql_real_escape_string($options['name']));
			
		//value (as a string)
		if(array_key_exists('value', $options))
			$wheres[]= sprintf("$tbl_prefix_ms2.`string`= '%s'", mysql_real_escape_string($options['value']));			

		//name_id (as an integer)
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`name_id`= %s", mysql_real_escape_string($options['name_id']));
			
		//value_id (as an integer)
		if(array_key_exists('value_id', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`value_id`= %s", mysql_real_escape_string($options['value_id']));
			
		//value_id (as an integer)
		if(array_key_exists('value_type', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`value_type`= '%s'", mysql_real_escape_string($options['value_type']));			
			
		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`owner_guid`= %s", mysql_real_escape_string($options['owner_guid']));			

		//access_id
		if(array_key_exists('access_id', $options))
			$wheres[]= sprintf("$tbl_prefix_md.`access_id`= %s", mysql_real_escape_string($options['access_id']));			
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "$tbl_prefix_md.`time_created` ".blg_dbschema_query_time_created($options);
			
		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "$tbl_prefix_md.`time_updated` ".blg_dbschema_query_time_updated($options);			
		
		//enabled
		if(array_key_exists('enabled', $options)){
			if($options['enabled']!==NULL)
				$wheres[]= sprintf("$tbl_prefix_md.`enabled`= '%s'", mysql_real_escape_string($options['enabled']));
		}
		elseif($strict_enabled)
			$wheres[]= "$tbl_prefix_md.`enabled`= 'yes'";
			
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select= blg_dbschema_query_fields($fieldsMetaData, $tbl_prefix_md).", $tbl_prefix_ms1.`string` AS 'name', $tbl_prefix_ms2.`string` AS 'value'";
	$query->from= 	"{$db_prefix}metadata {$tbl_prefix_md}, {$db_prefix}metastrings {$tbl_prefix_ms1}, {$db_prefix}metastrings {$tbl_prefix_ms2}";
	$query->where= 	"{$tbl_prefix_md}.`name_id`= {$tbl_prefix_ms1}.`id` AND {$tbl_prefix_md}.`value_id`= {$tbl_prefix_ms2}.`id` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_md.`id`";
	
	return $query;
}


/**
 * Select annotations
 * 
 * @param array	$options		
 * @param bool 	$strict_enabled	If true (default), returns only enabled annotations
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_annotations($options= NULL, $fieldsAnnotations= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_an= 'an';
	$tbl_prefix_ms1= 'ms1';
	$tbl_prefix_ms2= 'ms2';
	
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`id`= %s", mysql_real_escape_string($options['id']));		
		
		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`entity_guid`= %s", mysql_real_escape_string($options['entity_guid']));
			
		//name (as a string)
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("$tbl_prefix_ms1.`string`= '%s'", mysql_real_escape_string($options['name']));
			
		//value (as a string)
		if(array_key_exists('value', $options))
			$wheres[]= sprintf("$tbl_prefix_ms2.`string`= '%s'", mysql_real_escape_string($options['value']));

		//name_id (as an integer)
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`name_id`= %s", mysql_real_escape_string($options['name_id']));
			
		//value_id (as an integer)
		if(array_key_exists('value_id', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`value_id`= %s", mysql_real_escape_string($options['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`value_type`= %s", mysql_real_escape_string($options['value_type']));		
			
		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`owner_guid`= %s", mysql_real_escape_string($options['owner_guid']));			

		//access_id
		if(array_key_exists('access_id', $options))
			$wheres[]= sprintf("$tbl_prefix_an.`access_id`= %s", mysql_real_escape_string($options['access_id']));			
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "$tbl_prefix_an.`time_created` ".blg_dbschema_query_time_created($options);

		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "$tbl_prefix_md.`time_updated` ".blg_dbschema_query_time_updated($options);			
			
		//enabled
		if(array_key_exists('enabled', $options)){
			if($options['enabled']!==NULL)
				$wheres[]= sprintf("$tbl_prefix_an.`enabled`= '%s'", mysql_real_escape_string($options['enabled']));				
		}
		elseif($strict_enabled)
			$wheres[]= "$tbl_prefix_an.`enabled`= 'yes'";
		
		//where
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;		
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select= blg_dbschema_query_fields($fieldsAnnotations, $tbl_prefix_an).", $tbl_prefix_ms1.`string` AS 'name', $tbl_prefix_ms2.`string` AS 'value'";		
	$query->from= 	"{$db_prefix}annotations {$tbl_prefix_an}, {$db_prefix}metastrings {$tbl_prefix_ms1}, {$db_prefix}metastrings {$tbl_prefix_ms2}";
	$query->where= 	"{$tbl_prefix_an}.`name_id`= {$tbl_prefix_ms1}.`id` AND {$tbl_prefix_an}.`value_id`= {$tbl_prefix_ms2}.`id` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_an.`id`";
	
	return $query;
}

/**
 * Select realtionships
 * 
 * @param array	$options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_relationships($options= NULL, $fields= NULL){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix= 'rs';
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix.`id`= %s", mysql_real_escape_string($options['id']));		
			
		//guid (will search in `guid_one` and `guid_two`)
		if(array_key_exists('guid', $options)){
			$wheres[]= sprintf("( $tbl_prefix.`guid_one`= %s OR rs.`guid_two`= %s )", mysql_real_escape_string($options['guid']), mysql_real_escape_string($options['guid']));
		}
		else{
			if(array_key_exists('guid_one', $options))
				$wheres[]= sprintf("$tbl_prefix.`guid_one`= %s", mysql_real_escape_string($options['guid_one']));

			if(array_key_exists('guid_two', $options))
				$wheres[]= sprintf("$tbl_prefix.`guid_two`= %s", mysql_real_escape_string($options['guid_two']));
		}
		
		//relationship
		if(array_key_exists('relationship', $options))
			$wheres[]= sprintf("$tbl_prefix.`relationship`= '%s'", mysql_real_escape_string($options['relationship']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "$tbl_prefix.`time_created` ".blg_dbschema_query_time_created($options);		
			
		//where
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}

	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}entity_relationships {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`id`";	 
	
	return $query;
}

/**
 * Select subtypes
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_select_subtypes($options= NULL, $fields= NULL){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix= 'st';
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("$tbl_prefix.`id`= %s", mysql_real_escape_string($options['id']));

		//type
		if(array_key_exists('type', $options))
			$wheres[]= sprintf("$tbl_prefix.`type`= '%s'", mysql_real_escape_string($options['type']));

		//subtype
		if(array_key_exists('subtype', $options))
			$wheres[]= sprintf("$tbl_prefix.`subtype`= '%s'", mysql_real_escape_string($options['subtype']));
			
		//class
		if(array_key_exists('class', $options))
			$wheres[]= sprintf("$tbl_prefix.`class`= '%s'", mysql_real_escape_string($options['class']));			
			
		//where
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}

	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}entity_subtypes {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`id`";	 
	
	return $query;	
}

/**
 * Returns an array of where clauses
 * 
 * @param array $options
 * @param bool $strict_enabled
 * @return array WHERE clauses
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_wheres_entities($options, $tbl_prefix, $strict_enabled= TRUE){
	
	$wheres= array();
	
	//guid
	if(array_key_exists('guid', $options))
		$wheres[]= sprintf("$tbl_prefix.`guid`= %s", mysql_real_escape_string($options['guid']));
		
	//type
	if(array_key_exists('type', $options))
		$wheres[]= sprintf("$tbl_prefix.`type`= '%s'", mysql_real_escape_string($options['type']));
		
	//subtype
	if(array_key_exists('subtype', $options))
		$wheres[]= sprintf("$tbl_prefix.`subtype`= %s", mysql_real_escape_string($options['subtype']));
		
	//owner_guid
	if(array_key_exists('owner_guid', $options))
		$wheres[]= sprintf("$tbl_prefix.`owner_guid`= %s", mysql_real_escape_string($options['owner_guid']));

	//site_guid
	if(array_key_exists('site_guid', $options))
		$wheres[]= sprintf("$tbl_prefix.`site_guid`= %s", mysql_real_escape_string($options['site_guid']));				

	//container_guid
	if(array_key_exists('container_guid', $options))
		$wheres[]= sprintf("$tbl_prefix.`container_guis`= %s", mysql_real_escape_string($options['container_guis']));		
		
	//access_id
	if(array_key_exists('access_id', $options))
		$wheres[]= sprintf("$tbl_prefix.`access_id`= %s", mysql_real_escape_string($options['access_id']));
				
	//time_created
	if(array_key_exists('time_created', $options))
		$wheres[]= "$tbl_prefix.`time_created` ".blg_dbschema_query_time_created($options);

	//time_updated
	if(array_key_exists('time_updated', $options))
		$wheres[]= "$tbl_prefix.`time_updated` ".blg_dbschema_query_time_updated($options);			

	//last_action
	if(array_key_exists('time_updated', $options))
		$wheres[]= "$tbl_prefix.`time_updated` ".blg_dbschema_query_last_action($options);			
		
	//enabled
	if(array_key_exists('enabled', $options)){
		if($options['enabled']!==NULL)
			$wheres[]= sprintf("$tbl_prefix.`enabled`= '%s'", mysql_real_escape_string($options['enabled']));
	}
	elseif($strict_enabled)
		$wheres[]= "$tbl_prefix.`enabled`= 'yes'";	
		
	return $wheres;
}

/**
 * Select entities
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_entities($options= NULL, $fields= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix= 'en';
	
	if(is_array($options)){
		$wheres= blg_dbschema_wheres_entities($options, $tbl_prefix, $strict_enabled);
		
		//where
		$where= blg_dbschema_query_where($wheres);
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fields, $tbl_prefix);
	$query->from= 	"{$db_prefix}entities {$tbl_prefix}";
	$query->where= 	$where;
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix.`guid`";
	
	return $query;	
}

/**
 * Select object entities
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_objects($options= NULL, $fieldsEntities= NULL, $fieldsObjects= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_en= 'en';
	$tbl_prefix_ob= 'en_ob';
	
	if(is_array($options)){
		$wheres= blg_dbschema_wheres_entities($options, $tbl_prefix_en, $strict_enabled);
		
		if(array_key_exists('title', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("$tbl_prefix_ob.`title`= '%s'", mysql_real_escape_string($options['title']));
			
			
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST 	
			$wheres[]= sprintf("$tbl_prefix_ob.`description`= '%s'", mysql_real_escape_string($options['description']));	
			
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;		
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fieldsEntities, $tbl_prefix_en).', '.blg_dbschema_query_fields($fieldsObjects, $tbl_prefix_ob);
	$query->from= 	"{$db_prefix}entities {$tbl_prefix_en}, {$db_prefix}objects_entity {$tbl_prefix_ob}";
	$query->where= 	"$tbl_prefix_en.`guid` = $tbl_prefix_ob.`guid` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_en.`guid`";
	
	return $query;		
}

/**
 * Select user entities
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_users($options= NULL, $fieldsEntities= NULL, $fieldsUsers= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_en= 'en';
	$tbl_prefix_us= 'en_us';
	
	if(is_array($options)){
		$wheres= blg_dbschema_wheres_entities($options, $tbl_prefix_en, $strict_enabled);
		
		//name
		if(array_key_exists('name', $options))
			//TODO MATCH AGAINST maybe? 
			$wheres[]= sprintf("$tbl_prefix_us.`name`= '%s'", mysql_real_escape_string($options['name']));
			
		//surname
		if(array_key_exists('surname', $options))
			//TODO MATCH AGAINST maybe? 
			$wheres[]= sprintf("$tbl_prefix_us.`surname`= '%s'", mysql_real_escape_string($options['surname']));

		//username
		if(array_key_exists('username', $options))
			$wheres[]= sprintf("$tbl_prefix_us.`username`= '%s'", mysql_real_escape_string($options['username']));

		//email
		if(array_key_exists('email', $options)) 
			$wheres[]= sprintf("$tbl_prefix_us.`email`= '%s'", mysql_real_escape_string($options['email']));

		//language
		if(array_key_exists('language', $options)) 
			$wheres[]= sprintf("$tbl_prefix_us.`language`= '%s'", mysql_real_escape_string($options['language']));

		//code
		if(array_key_exists('code', $options)) 
			$wheres[]= sprintf("$tbl_prefix_us.`code`= '%s'", mysql_real_escape_string($options['code']));			

		//banned
		if(array_key_exists('banned', $options))
			$wheres[]= sprintf("$tbl_prefix_us.`banned`= '%s'", mysql_real_escape_string($options['banned']));	

		//admin
		if(array_key_exists('admin', $options))
			$wheres[]= sprintf("$tbl_prefix_us.`admin`= '%s'", mysql_real_escape_string($options['admin']));	

		//prev_last_action
		if(array_key_exists('prev_last_action', $options))
			$wheres[]= "$tbl_prefix_us.`prev_last_action` ".blg_dbschema_query_prev_last_action($options);

		//last_login
		if(array_key_exists('last_login', $options))
			$wheres[]= "$tbl_prefix_us.`last_action` ".blg_dbschema_query_last_login($options);

		//prev_last_login
		if(array_key_exists('prev_last_login', $options))
			$wheres[]= "$tbl_prefix_us.`prev_last_action` ".blg_dbschema_query_prev_last_login($options);			
			
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fieldsEntities, $tbl_prefix_en).', '.blg_dbschema_query_fields($fieldsUsers, $tbl_prefix_us);	
	$query->from= 	"{$db_prefix}entities {$tbl_prefix_en}, {$db_prefix}users_entity {$tbl_prefix_us}";
	$query->where= 	"{$tbl_prefix_en}.`guid` = {$tbl_prefix_us}.`guid` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_en.`guid`";
	
	return $query;		
}

/**
 * Select site entities
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_sites($options= NULL, $fieldsEntities= NULL, $fieldsSites= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_en= 'en';
	$tbl_prefix_si= 'en_si';
	
	if(is_array($options)){
		$wheres= blg_dbschema_wheres_entities($options, $tbl_prefix_en, $strict_enabled);
		
		//name
		if(array_key_exists('name', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("$tbl_prefix_si.`name`= '%s'", mysql_real_escape_string($options['name']));			

		//description
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST
			$wheres[]= sprintf("$tbl_prefix_si.`description`= '%s'", mysql_real_escape_string($options['description']));

		//url
		if(array_key_exists('url', $options))
			$wheres[]= sprintf("$tbl_prefix_si.`url`= '%s'", mysql_real_escape_string($options['url']));			

		//where
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;		
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fieldsEntities, $tbl_prefix_en).', '.blg_dbschema_query_fields($fieldsSites, $tbl_prefix_si);
	$query->from= 	"{$db_prefix}entities {$tbl_prefix_en}, {$db_prefix}sites_entity {$tbl_prefix_si}";
	$query->where= 	"{$tbl_prefix_en}.`guid` = {$tbl_prefix_si}.`guid` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_en.`guid`";
	
	return $query;		
}

/**
 * Select group entities
 * 
 * @param  $options
 * @return stdClass Query data
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_select_groups($options= NULL, $fieldsEntities= NULL, $fieldsGroups= NULL, $strict_enabled= TRUE){
	$db_prefix= blg_get_config('dbprefix');
	$tbl_prefix_en= 'en';
	$tbl_prefix_gr= 'en_gr';
	
	if(is_array($options)){
		$wheres= blg_dbschema_wheres_entities($options, $tbl_prefix_en, $strict_enabled);
		
		//name
		if(array_key_exists('name', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("$tbl_prefix_gr.`name`= '%s'", mysql_real_escape_string($options['name']));

		//description
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST 		
			$wheres[]= sprintf("$tbl_prefix_gr.`description`= '%s'", mysql_real_escape_string($options['description']));			
			
		//where
		$where= blg_dbschema_query_where($wheres);
		if(!empty($where))
			$where= 'AND '.$where;		
			
		//order
		$order= blg_dbschema_query_order($options);		
		
		//limit
		$limit= blg_dbschema_query_limit($options);
	}
	
	$query= new stdClass();
	$query->select=	blg_dbschema_query_fields($fieldsEntities, $tbl_prefix_en).', '.blg_dbschema_query_fields($fieldsGroups, $tbl_prefix_gr);	
	$query->from= 	"{$db_prefix}entities {$tbl_prefix_en}, {$db_prefix}groups_entity {$tbl_prefix_gr}";
	$query->where= 	"{$tbl_prefix_en}.`guid` = {$tbl_prefix_gr}.`guid` $where";
	$query->order= 	$order;
	$query->limit= 	$limit;
	$query->count_field= "$tbl_prefix_en.`guid`";
	
	return $query;		
}

/***********************************************************
 *						INSERT QUERIES
 ***********************************************************/

/**
 * Insert message row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_message($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//from_guid
		if(array_key_exists('from_guid', $params))
			$values[]= sprintf("`from_guid` = %s", mysql_real_escape_string($params['from_guid']));

		//to_guid
		if(array_key_exists('to_guid', $params))
			$values[]= sprintf("`to_guid` = %s", mysql_real_escape_string($params['to_guid']));
			
		//subject
		if(array_key_exists('subject', $params))
			$values[]= sprintf("`subject` = %s", mysql_real_escape_string($params['subject']));

		//message
		if(array_key_exists('message', $params))
			$values[]= sprintf("`message` = '%s'", mysql_real_escape_string($params['message']));

		$time= time();			
		//time_created
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".$time;
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated` = %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".$time;		
			
		//status
		if(array_key_exists('status', $params))
			$values[]= sprintf("`status` = '%s'", mysql_real_escape_string($params['status']));

		//reply_id
		if(array_key_exists('reply_id', $params))
			$values[]= sprintf("`reply_id` = %s", mysql_real_escape_string(empty($params['reply_id'])?'NULL':$params['reply_id']));			
			
		$values= blg_dbschema_query_set_values($values);	
	}
					
	$query= new stdClass();
	$query->insert= "`{$prefix}messages`";
	$query->values= $values;
	return $query;
}

/**
 * Insert configuration row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_config($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//param
		if(array_key_exists('param', $params))
			$values[]= sprintf("`param` = '%s'", mysql_real_escape_string($params['param']));
			
		//value
		if(array_key_exists('value', $params))
			$values[]= sprintf("`value` = '%s'", mysql_real_escape_string($params['value']));
			
		$values= blg_dbschema_query_set_values($values);	
	}
					
	$query= new stdClass();
	$query->insert= "`{$prefix}config`";
	$query->values= $values;
	return $query;
}

/**
 * Insert files row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_file($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//url
		if(array_key_exists('url', $params))
			$values[]= sprintf("`url` = '%s'", mysql_real_escape_string($params['url']));
			
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));

		//name
		if(array_key_exists('original_name', $params))
			$values[]= sprintf("`original_name` = '%s'", mysql_real_escape_string($params['original_name']));			
	
		//extension
		if(array_key_exists('ext', $params))	
			$values[]= sprintf("`ext` = '%s'", mysql_real_escape_string($params['ext']));
			
		//size
		if(array_key_exists('size', $params))	
			$values[]= sprintf("`size` = '%s'", mysql_real_escape_string($params['size']));			
	
		//mime-type
		if(array_key_exists('mime', $params))
			$values[]= sprintf("`mime` = '%s'", mysql_real_escape_string($params['mime']));
	
		//data
		if(array_key_exists('data', $params))
			$values[]= sprintf("`data` = '%s'", mysql_real_escape_string($params['data']));
			
		//owner
		if(array_key_exists('owner', $params))
			$values[]= sprintf("`owner` = %s", mysql_real_escape_string($params['owner']));			

		$time= time();			
		//time_created
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".$time;
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated` = %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".$time;		

		//info
		if(array_key_exists('info', $params))
			$values[]= sprintf("`info` = '%s'", mysql_real_escape_string($params['info']));			
	
		$values= blg_dbschema_query_set_values($values);	
	}
					
	$query= new stdClass();
	$query->insert= "`{$prefix}files`";
	$query->values= $values;
	return $query;
}

/**
 * Insert metastring row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_metastring($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();

		//string
		if(array_key_exists('string', $params))
			$values[]= sprintf("`string` = '%s'", mysql_real_escape_string($params['string']));
			
		$values= blg_dbschema_query_set_values($values);
	}
					
	$query= new stdClass();
	$query->insert= "`{$prefix}metastrings`";
	$query->values= $values;
	return $query;
}

/**
 * Insert metadata row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_metadata($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//entity_guid
		if(array_key_exists('entity_guid', $params))
			$values[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($params['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $params))
			$values[]= sprintf("`name_id` = %s", mysql_real_escape_string($params['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $params))	
			$values[]= sprintf("`value_id` = %s", mysql_real_escape_string($params['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $params))	
			$values[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($params['value_type']));			
	
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($params['owner_guid']));
	
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id` = %s", mysql_real_escape_string($params['access_id']));

		//time_created
		$time= time();
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".$time;
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated` = %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".$time;			
	
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($params['enabled']));
		else
			$values[]= "`enabled` = 'yes'";
			
		$values= blg_dbschema_query_set_values($values);	
	}
					
	$query= new stdClass();
	$query->insert= "`{$prefix}metadata`";
	$query->values= $values;
	return $query;
}

/**
 * Insert annotation row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_annotation($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//entity_guid
		if(array_key_exists('entity_guid', $params))
			$values[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($params['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $params))
			$values[]= sprintf("`name_id` = %s", mysql_real_escape_string($params['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $params))	
			$values[]= sprintf("`value_id` = %s", mysql_real_escape_string($params['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $params))	
			$values[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($params['value_type']));
	
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($params['owner_guid']));
	
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id` = %s", mysql_real_escape_string($params['access_id']));
			
		//time_created
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".time();		

		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated` = %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".$time;				
	
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($params['enabled']));
		else
			$values[]= "`enabled` = 'yes'";	
			
		$values= blg_dbschema_query_set_values($values);
	}

	$query= new stdClass();
	$query->insert= "`{$prefix}annotations`";
	$query->values= $values;
	return $query;
}


/**
 * Insert relationship row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_relationship($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();

		//guid_one
		if(array_key_exists('guid_one', $params))
			$values[]= sprintf("`guid_one` = %s", mysql_real_escape_string($params['guid_one']));
	
		//relationship
		if(array_key_exists('relationship', $params))
			$values[]= sprintf("`relationship` = '%s'", mysql_real_escape_string($params['relationship']));
			
		//guid_two
		if(array_key_exists('guid_two', $params))
			$values[]= sprintf("`guid_two` = %s", mysql_real_escape_string($params['guid_two']));
			
		//time_created
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".time();			
				
		$values= blg_dbschema_query_set_values($values);
	}	

	$query= new stdClass();
	$query->insert= "`{$prefix}entity_relationships`";
	$query->values= $values;
	return $query;
}


/**
 * Insert subtype row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_subtype($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();

		//type
		if(array_key_exists('type', $params))
			$values[]= sprintf("`type` = '%s'", mysql_real_escape_string($params['type']));
	
		//subtype
		if(array_key_exists('subtype', $params))
			$values[]= sprintf("`subtype` = '%s'", mysql_real_escape_string($params['subtype']));
			
		//class
		if(array_key_exists('class', $params))
			$values[]= sprintf("`class` = '%s'", mysql_real_escape_string($params['class']));
			
		$values= blg_dbschema_query_set_values($values);
	}	

	$query= new stdClass();
	$query->insert= "`{$prefix}entity_subtypes`";
	$query->values= $values;
	return $query;
}

/**
 * Insert entity row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_entity($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//type
		if(array_key_exists('type', $params))
			$values[]= sprintf("`type` = '%s'", mysql_real_escape_string($params['type']));
			
		//subtype
		if(array_key_exists('subtype', $params) && !empty($params['subtype']))
			$values[]= sprintf("`subtype` = %s", mysql_real_escape_string($params['subtype']));	
	
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($params['owner_guid']));
	
		//site_guid
		if(array_key_exists('site_guid', $params))
			$values[]= sprintf("`site_guid` = %s", mysql_real_escape_string($params['site_guid']));
	
		//container_guid
		if(array_key_exists('container_guid', $params))
			$values[]= sprintf("`container_guid` = %s", mysql_real_escape_string($params['container_guid']));
	
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id` = %s", mysql_real_escape_string($params['access_id']));

		//time_created
		if(array_key_exists('time_created', $params))
			$values[]= sprintf("`time_created` = %s", mysql_real_escape_string($params['time_created']));
		else
			$values[]= "`time_created` = ".time();

		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated` = %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = 0";			
	
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($params['enabled']));
		else
			$values[]= "`enabled` = 'yes'";	
			
		$values= blg_dbschema_query_set_values($values);
	}	

	$query= new stdClass();
	$query->insert= "`{$prefix}entities`";
	$query->values= $values;
	return $query;
}

/**
 * Insert object row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_object($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array(($params))){
		$values= array();
		
		//guid
		if(array_key_exists('guid', $params))
			$values[]= sprintf("`guid` = %s", mysql_real_escape_string($params['guid']));			
	
		//title
		if(array_key_exists('title', $params))
			$values[]= sprintf("`title` = '%s'", mysql_real_escape_string($params['title']));
			
		//description
		if(array_key_exists('description', $params))
			$values[]= sprintf("`description` = '%s'", mysql_real_escape_string($params['description']));		
			
		$values= blg_dbschema_query_set_values($values);	
	}

	$query= new stdClass();
	$query->insert= "`{$prefix}objects_entity`";
	$query->values= $values;
	return $query;
}

/**
 * Insert user row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_user($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//guid
		if(array_key_exists('guid', $params))
			$values[]= sprintf("`guid` = %s", mysql_real_escape_string($params['guid']));			
	
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));
			
		//surname
		if(array_key_exists('surname', $params))
			$values[]= sprintf("`surname` = '%s'", mysql_real_escape_string($params['surname']));			
			
		//username
		if(array_key_exists('username', $params))
			$values[]= sprintf("`username` = '%s'", mysql_real_escape_string($params['username']));	
	
		//password
		if(array_key_exists('password', $params))
			$values[]= sprintf("`password` = '%s'", mysql_real_escape_string($params['password']));
	
		//salt
		if(array_key_exists('salt', $params))
			$values[]= sprintf("`salt` = '%s'", mysql_real_escape_string($params['salt']));
			
		//email
		if(array_key_exists('email', $params))
			$values[]= sprintf("`email` = '%s'", mysql_real_escape_string($params['email']));
	
		//language
		if(array_key_exists('language', $params))
			$values[]= sprintf("`language` = '%s'", mysql_real_escape_string($params['language']));
			
		//code
		if(array_key_exists('code', $params))
			$values[]= sprintf("`code` = '%s'", mysql_real_escape_string($params['code']));
					
		//banned
		if(array_key_exists('banned', $params))
			$values[]= sprintf("`banned` = '%s'", mysql_real_escape_string($params['banned']));
	
		//admin
		if(array_key_exists('admin', $params))
			$values[]= sprintf("`admin` = '%s'", mysql_real_escape_string($params['admin']));
			
		//prev_last_action
		if(array_key_exists('prev_last_action', $params))
			$values[]= sprintf("`prev_last_action` = %s", mysql_real_escape_string($params['prev_last_action']));
		else
			$values[]= "`prev_last_action` = 0";	

		//last_login
		if(array_key_exists('last_login', $params))
			$values[]= sprintf("`last_login` = %s", mysql_real_escape_string($params['last_login']));
		else
			$values[]= "`last_login` = 0";
			
		//prev_last_login
		if(array_key_exists('prev_last_login', $params))
			$values[]= sprintf("`prev_last_login` = %s", mysql_real_escape_string($params['prev_last_login']));
		else
			$values[]= "`prev_last_login` = 0";			
			
		$values= blg_dbschema_query_set_values($values);
	}

	$query= new stdClass();
	$query->insert= "`{$prefix}users_entity`";
	$query->values= $values;
	return $query;
}

/**
 * Insert site row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_site($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();

		//guid
		if(array_key_exists('guid', $params))
			$values[]= sprintf("`guid` = %s", mysql_real_escape_string($params['guid']));			
	
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));
			
		//description
		if(array_key_exists('description', $params))
			$values[]= sprintf("`description` = '%s'", mysql_real_escape_string($params['description']));		
	
		//url
		if(array_key_exists('url', $params))
			$values[]= sprintf("`url` = '%s'", mysql_real_escape_string($params['url']));		
							
		$values= blg_dbschema_query_set_values($values);
	}	

	$query= new stdClass();
	$query->insert= "`{$prefix}sites_entity`";
	$query->values= $values;
	return $query;
}

/**
 * Insert group row query
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_insert_group($params= NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($params)){
		$values= array();
		
		//guid
		if(array_key_exists('guid', $params))
			$values[]= sprintf("`guid` = %s", mysql_real_escape_string($params['guid']));	
	
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));
			
		//description
		if(array_key_exists('description', $params))
			$values[]= sprintf("`description` = '%s'", mysql_real_escape_string($params['description']));			
			
		$values= blg_dbschema_query_set_values($values);
	}

	$query= new stdClass();
	$query->insert= "`{$prefix}groups_entity`";
	$query->values= $values;
	return $query;
}

/***********************************************************
 * 					  	   UPDATES
 ***********************************************************/

/**
 * Update messages
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_messages($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
			
		//from_guid
		if(array_key_exists('from_guid', $params))
			$values[]= sprintf("`from_guid` = %s", mysql_real_escape_string($params['from_guid']));
			
		//to_guid
		if(array_key_exists('to_guid', $params))
			$values[]= sprintf("`to_guid` = %s", mysql_real_escape_string($params['to_guid']));
			
		//subject
		if(array_key_exists('subject', $params))
			$values[]= sprintf("`subject` = %s", mysql_real_escape_string($params['subject']));
			
		//message
		if(array_key_exists('message', $params))
			$values[]= sprintf("`message` = '%s'", mysql_real_escape_string($params['message']));		
			
		//time_updated
		if(array_key_exists('time_updated', $params) && !empty($params['time_updated']))
			$values[]= sprintf("`time_updated`= %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".time();		

		//status
		if(array_key_exists('status', $params))
			$values[]= sprintf("`status` = '%s'", mysql_real_escape_string($params['status']));
			
		//reply_id
		if(array_key_exists('reply_id', $params))
			$values[]= sprintf("`reply_id` = %s", mysql_real_escape_string(empty($params['reply_id'])?'NULL':$params['reply_id']));
			
		$values= blg_dbschema_query_set_values($values);	
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();

		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
	
		//from_guid
		if(array_key_exists('from_guid', $options))
			$wheres[]= sprintf("`from_guid` = %s", mysql_real_escape_string($options['from_guid']));
			
		//to_guid
		if(array_key_exists('to_guid', $options))
			$wheres[]= sprintf("`to_guid` = %s", mysql_real_escape_string($options['to_guid']));

		//subject
		if(array_key_exists('subject', $options))
			$wheres[]= sprintf("`subject` = %s", mysql_real_escape_string($options['subject']));

		//message
		if(array_key_exists('message', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`message`= '%s'", mysql_real_escape_string($options['message']));			
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
			
		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_time_updated($options);
			
		//status
		if(array_key_exists('status', $options))
			$wheres[]= sprintf("`status` LIKE '%s'", mysql_real_escape_string($options['status']));

		//reply_id
		if(array_key_exists('reply_id', $options))
			$wheres[]= sprintf("`reply_id` = %s", mysql_real_escape_string(empty($options['reply_id'])?'NULL':$options['reply_id']));

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}messages`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update configuration
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_config($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
				
		//value
		if(array_key_exists('value', $params))
			$values[]= sprintf("`value` = '%s'", mysql_real_escape_string($params['value']));
			
		$values= blg_dbschema_query_set_values($values);	
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();

		//param
		if(array_key_exists('param', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`param`= '%s'", mysql_real_escape_string($options['param']));

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}config`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update files
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_files($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
		
		//url
		if(array_key_exists('url', $params))
			$values[]= sprintf("`url` = '%s'", mysql_real_escape_string($params['url']));
			
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));

		//original_name
		if(array_key_exists('original_name', $params))
			$values[]= sprintf("`original_name` = '%s'", mysql_real_escape_string($params['original_name']));			
	
		//extension
		if(array_key_exists('ext', $params))	
			$values[]= sprintf("`ext` = '%s'", mysql_real_escape_string($params['ext']));
			
		//size
		if(array_key_exists('size', $params))	
			$values[]= sprintf("`size` = '%s'", mysql_real_escape_string($params['size']));
	
		//mime-type
		if(array_key_exists('mime', $params))
			$values[]= sprintf("`mime` = '%s'", mysql_real_escape_string($params['mime']));
	
		//data
		if(array_key_exists('data', $params))
			$values[]= sprintf("`data` = '%s'", mysql_real_escape_string($params['data']));
	
		//owner
		if(array_key_exists('owner', $params))
			$values[]= sprintf("`owner` = %s", mysql_real_escape_string($params['owner']));
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated`= %s", mysql_real_escape_string($params['time_updated']));

		//info
		if(array_key_exists('info', $params))
			$values[]= sprintf("`info` = '%s'", mysql_real_escape_string($params['info']));			
			
		$values= blg_dbschema_query_set_values($values);	
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();

		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));		
		
		//url
		if(array_key_exists('url', $options))
			$wheres[]= sprintf("`url` = '%s'", mysql_real_escape_string($options['url']));
			
		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("`name` = '%s'", mysql_real_escape_string($options['name']));	
	
		//extension
		if(array_key_exists('ext', $options))	
			$wheres[]= sprintf("`ext` = '%s'", mysql_real_escape_string($options['ext']));
			
		//size
		if(array_key_exists('size', $options))	
			$wheres[]= sprintf("`size` = '%s'", mysql_real_escape_string($options['size']));			
	
		//mime
		if(array_key_exists('mime', $options))
			$wheres[]= sprintf("`mime` = '%s'", mysql_real_escape_string($options['mime']));
	
		//owner
		if(array_key_exists('owner', $options))
			$wheres[]= sprintf("`owner` = %s", mysql_real_escape_string($options['owner']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
			
		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_time_updated($options);

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}files`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update metastrings
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_metastrings($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
		
		//string
		if(array_key_exists('string', $options))
			$values[]= sprintf("`string` = '%s'", mysql_real_escape_string($params['string']));

		$values= blg_dbschema_query_set_values($values);
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();

		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));	
		
		//string
		if(array_key_exists('string', $options))
			$wheres[]= sprintf("`string` = '%s'", mysql_real_escape_string($options['string']));

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}metastrings`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update metadata
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_metadata($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
		
		//entity_guid
		if(array_key_exists('entity_guid', $params))
			$values[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($params['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $params))
			$values[]= sprintf("`name_id` = %s", mysql_real_escape_string($params['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $params))	
			$values[]= sprintf("`value_id` = %s", mysql_real_escape_string($params['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $params))	
			$values[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($params['value_type']));
	
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($params['owner_guid']));
	
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id` = %s", mysql_real_escape_string($params['access_id']));
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated`= %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".time();				
	
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($params['enabled']));
			
		$values= blg_dbschema_query_set_values($values);	
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();

		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));		
		
		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($options['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("`name_id` = %s", mysql_real_escape_string($options['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $options))	
			$wheres[]= sprintf("`value_id` = %s", mysql_real_escape_string($options['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $options))	
			$wheres[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($options['value_type']));			
	
		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($options['owner_guid']));
	
		//access_id
		if(array_key_exists('access_id', $options))
			$wheres[]= sprintf("`access_id` = %s", mysql_real_escape_string($options['access_id']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
	
		//enabled
		if(array_key_exists('enabled', $options))
			$wheres[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($options['enabled']));
		else
			$wheres[]= "`enabled` = 'yes'";

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}metadata`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update annotations
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_annotations($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();	
		
		//entity_guid
		if(array_key_exists('entity_guid', $params))
			$values[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($params['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $params))
			$values[]= sprintf("`name_id` = %s", mysql_real_escape_string($params['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $params))	
			$values[]= sprintf("`value_id` = %s", mysql_real_escape_string($params['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $params))	
			$values[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($params['value_type']));			
	
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($params['value_type']));
	
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id` = %s", mysql_real_escape_string($params['access_id']));
			
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated`= %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".time();				
	
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($params['enabled']));
			
		$values= blg_dbschema_query_set_values($values);	
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));		

		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($options['entity_guid']));
			
		//name_id
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("`name_id` = %s", mysql_real_escape_string($options['name_id']));	
	
		//value_id
		if(array_key_exists('value_id', $options))	
			$wheres[]= sprintf("`value_id` = %s", mysql_real_escape_string($options['value_id']));
			
		//value_type
		if(array_key_exists('value_type', $options))
			$wheres[]= sprintf("`value_type` = '%s'", mysql_real_escape_string($options['value_type']));			
	
		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($options['owner_guid']));
	
		//access_id
		if(array_key_exists('access_id', $options))
			$wheres[]= sprintf("`access_id` = %s", mysql_real_escape_string($options['access_id']));
	
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
			
		//enabled
		if(array_key_exists('enabled', $options))
			$wheres[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($options['enabled']));
		else
			$wheres[]= "`enabled` = 'yes'";			

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}annotations`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update relationships
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_relationships($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();

		//guid_one
		if(array_key_exists('guid_one', $params))
			$values[]= sprintf("`guid_one` = %s", mysql_real_escape_string($params['guid_one']));
	
		//relationship
		if(array_key_exists('relationship', $params))
			$values[]= sprintf("`relationship` = '%s'", mysql_real_escape_string($params['relationship']));
			
		//guid_two
		if(array_key_exists('guid_two', $params))
			$values[]= sprintf("`guid_two` = %s", mysql_real_escape_string($params['guid_two']));
				
		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();

		//guid_one
		if(array_key_exists('guid_one', $options))
			$wheres[]= sprintf("`guid_one` = %s", mysql_real_escape_string($options['guid_one']));
	
		//relationship
		if(array_key_exists('relationship', $options))
			$wheres[]= sprintf("`relationship` = '%s'", mysql_real_escape_string($options['relationship']));
			
		//guid_two
		if(array_key_exists('guid_two', $options))
			$wheres[]= sprintf("`guid_two` = %s", mysql_real_escape_string($options['guid_two']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
				
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}entity_relationships`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}


/**
 * Update subtypes
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_subtypes($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();

		//type
		if(array_key_exists('type', $params))
			$values[]= sprintf("`type`= '%s'", mysql_real_escape_string($params['type']));

		//subtype
		if(array_key_exists('subtype', $params))
			$values[]= sprintf("`subtype`= '%s'", mysql_real_escape_string($params['subtype']));
			
		//class
		if(array_key_exists('class', $params))
			$values[]= sprintf("`class`= '%s'", mysql_real_escape_string($params['class']));
				
		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();

		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id`= %s", mysql_real_escape_string($options['id']));

		//type
		if(array_key_exists('type', $options))
			$wheres[]= sprintf("`type`= '%s'", mysql_real_escape_string($options['type']));

		//subtype
		if(array_key_exists('subtype', $options))
			$wheres[]= sprintf("`subtype`= '%s'", mysql_real_escape_string($options['subtype']));
			
		//class
		if(array_key_exists('class', $options))
			$wheres[]= sprintf("`class`= '%s'", mysql_real_escape_string($options['class']));
				
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}entity_subtypes`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}

/**
 * Update entities
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_entities($options=NULL, $params=NULL, $strict_enabled= TRUE){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();
			
		//type
		if(array_key_exists('type', $params))
			$values[]= sprintf("`type`= '%s'", mysql_real_escape_string($params['type']));
			
		//subtype
		if(array_key_exists('subtype', $params))
			$values[]= sprintf("`subtype`= %s", mysql_real_escape_string($params['subtype']));
			
		//owner_guid
		if(array_key_exists('owner_guid', $params))
			$values[]= sprintf("`owner_guid`= %s", mysql_real_escape_string($params['owner_guid']));
	
		//site_guid
		if(array_key_exists('site_guid', $params))
			$values[]= sprintf("`site_guid`= %s", mysql_real_escape_string($params['site_guid']));				
	
		//container_guid
		if(array_key_exists('container_guid', $params))
			$values[]= sprintf("`container_guis`= %s", mysql_real_escape_string($params['container_guis']));		
			
		//access_id
		if(array_key_exists('access_id', $params))
			$values[]= sprintf("`access_id`= %s", mysql_real_escape_string($params['access_id']));
						
		//time_updated
		if(array_key_exists('time_updated', $params))
			$values[]= sprintf("`time_updated`= %s", mysql_real_escape_string($params['time_updated']));
		else
			$values[]= "`time_updated` = ".time();			
	
		//last_action
		if(array_key_exists('last_action', $params))
			$values[]= sprintf("`last_action`= %s", mysql_real_escape_string($params['last_action']));			
			
		//enabled
		if(array_key_exists('enabled', $params))
			$values[]= sprintf("`enabled`= '%s'", mysql_real_escape_string($params['enabled']));			

		$values= blg_dbschema_query_set_values($values);
	}	

	//Rows to updates
	if(is_array($options)){
		$wheres= array();
	
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid`= %s", mysql_real_escape_string($options['guid']));
			
		//type
		if(array_key_exists('type', $options))
			$wheres[]= sprintf("`type`= '%s'", mysql_real_escape_string($options['type']));
			
		//subtype
		if(array_key_exists('subtype', $options))
			$wheres[]= sprintf("`subtype`= %s", mysql_real_escape_string($options['subtype']));
			
		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid`= %s", mysql_real_escape_string($options['owner_guid']));
	
		//site_guid
		if(array_key_exists('site_guid', $options))
			$wheres[]= sprintf("`site_guid`= %s", mysql_real_escape_string($options['site_guid']));				
	
		//container_guid
		if(array_key_exists('container_guid', $options))
			$wheres[]= sprintf("`container_guid`= %s", mysql_real_escape_string($options['container_guid']));		
			
		//access_id
		if(array_key_exists('access_id', $options))
			$wheres[]= sprintf("`access_id`= %s", mysql_real_escape_string($options['access_id']));
					
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);
	
		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_time_updated($options);			
	
		//last_action
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_last_action($options);			
			
		//enabled
		if(array_key_exists('enabled', $options)){
			$wheres[]= sprintf("`enabled`= '%s'", mysql_real_escape_string($options['enabled']));
		}
		elseif($strict_enabled)
			$wheres[]= "`enabled`= 'yes'";	

		$where= blg_dbschema_query_where($wheres);			
	}

	$query= new stdClass();
	$query->update= "`{$prefix}entities`";
	$query->values= $values;
	$query->where= $where;
	return $query;
}

/**
 * Update objects
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_objects($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();

		//title
		if(array_key_exists('title', $params)) 
			$values[]= sprintf("`title`= '%s'", mysql_real_escape_string($params['title']));
		
		//description
		if(array_key_exists('description', $params)) 	
			$values[]= sprintf("`description`= '%s'", mysql_real_escape_string($params['description']));	

		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options)) 
			$wheres[]= sprintf("`guid`= '%s'", mysql_real_escape_string($options['guid']));
				
		//title
		if(array_key_exists('title', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`title`= '%s'", mysql_real_escape_string($options['title']));
			
		//description
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST 	
			$wheres[]= sprintf("`description`= '%s'", mysql_real_escape_string($options['description']));	
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}objects_entity`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}

/**
 * Update users
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_users($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
		
	//Values to updated
	if(is_array($params)){
		$values= array();			
	
		//name
		if(array_key_exists('name', $params))
			$values[]= sprintf("`name` = '%s'", mysql_real_escape_string($params['name']));
			
		//surname
		if(array_key_exists('surname', $params))
			$values[]= sprintf("`surname` = '%s'", mysql_real_escape_string($params['surname']));			
			
		//username
		if(array_key_exists('username', $params))
			$values[]= sprintf("`username` = '%s'", mysql_real_escape_string($params['username']));	
	
		//password
		if(array_key_exists('password', $params))
			$values[]= sprintf("`password` = '%s'", mysql_real_escape_string($params['password']));
	
		//salt
		if(array_key_exists('salt', $params))
			$values[]= sprintf("`salt` = '%s'", mysql_real_escape_string($params['salt']));
			
		//email
		if(array_key_exists('email', $params))
			$values[]= sprintf("`email` = '%s'", mysql_real_escape_string($params['email']));
	
		//language
		if(array_key_exists('language', $params))
			$values[]= sprintf("`language` = '%s'", mysql_real_escape_string($params['language']));
			
		//code
		if(array_key_exists('code', $params))
			$values[]= sprintf("`code` = '%s'", mysql_real_escape_string($params['code']));
					
		//banned
		if(array_key_exists('banned', $params))
			$values[]= sprintf("`banned` = '%s'", mysql_real_escape_string($params['banned']));
	
		//admin
		if(array_key_exists('admin', $params))
			$values[]= sprintf("`admin` = '%s'", mysql_real_escape_string($params['admin']));	

		//prev_last_action
		if(array_key_exists('prev_last_action', $params))
			$values[]= sprintf("`prev_last_action` = %s", mysql_real_escape_string($params['prev_last_action']));
			
		//last_login
		if(array_key_exists('last_login', $params))
			$values[]= sprintf("`last_login` = %s", mysql_real_escape_string($params['last_login']));			

		//prev_last_login
		if(array_key_exists('prev_last_login', $params))
			$values[]= sprintf("`prev_last_login` = %s", mysql_real_escape_string($params['prev_last_login']));
		
		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));			
	
		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("`name` = '%s'", mysql_real_escape_string($options['name']));
			
		//name
		if(array_key_exists('surname', $options))
			$wheres[]= sprintf("`surname` = '%s'", mysql_real_escape_string($options['surname']));			
			
		//username
		if(array_key_exists('username', $options))
			$wheres[]= sprintf("`username` = '%s'", mysql_real_escape_string($options['username']));
			
		//email
		if(array_key_exists('email', $options))
			$wheres[]= sprintf("`email` = '%s'", mysql_real_escape_string($options['email']));
	
		//language
		if(array_key_exists('language', $options))
			$wheres[]= sprintf("`language` = '%s'", mysql_real_escape_string($options['language']));
			
		//code
		if(array_key_exists('code', $options))
			$wheres[]= sprintf("`code` = '%s'", mysql_real_escape_string($options['code']));
					
		//banned
		if(array_key_exists('banned', $options))
			$wheres[]= sprintf("`banned` = '%s'", mysql_real_escape_string($options['banned']));
	
		//admin
		if(array_key_exists('admin', $options))
			$wheres[]= sprintf("`admin` = '%s'", mysql_real_escape_string($options['admin']));	

		//prev_last_action
		if(array_key_exists('prev_last_action', $options))
			$wheres[]= "`prev_last_action` ".blg_dbschema_query_last_action($options);

		//last_login
		if(array_key_exists('last_login', $options))
			$wheres[]= "`last_login` ".blg_dbschema_query_last_login($options);

		//prev_last_login
		if(array_key_exists('prev_last_login', $options))
			$wheres[]= "`prev_last_login` ".blg_dbschema_query_prev_last_login($options);			
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}users_entity`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}

/**
 * Update sites
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_sites($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();

		//name
		if(array_key_exists('name', $params)) 
			$values[]= sprintf("`name`= '%s'", mysql_real_escape_string($params['name']));
		
		//description
		if(array_key_exists('description', $params)) 	
			$values[]= sprintf("en_ob.`description`= '%s'", mysql_real_escape_string($params['description']));

		//url
		if(array_key_exists('url', $params))
			$values[]= sprintf("`url`= '%s'", mysql_real_escape_string($params['url']));			

		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options)) 
			$wheres[]= sprintf("`guid`= %s", mysql_real_escape_string($options['guid']));
				
		//name
		if(array_key_exists('name', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`name`= '%s'", mysql_real_escape_string($options['name']));
			
		//description
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST 	
			$wheres[]= sprintf("`description`= '%s'", mysql_real_escape_string($options['description']));	
			
		//url
		if(array_key_exists('url', $options))
			//TODO Change this to MATCH AGAINST 	
			$wheres[]= sprintf("`url`= '%s'", mysql_real_escape_string($options['url']));
						
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}sites_entity`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}

/**
 * Update groups
 * 
 * @param $options
 * @param $params
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_update_groups($options=NULL, $params=NULL){
	$prefix= blg_get_config('dbprefix');
	
	//Values to updated
	if(is_array($params)){
		$values= array();

		//name
		if(array_key_exists('name', $params)) 
			$values[]= sprintf("`name`= '%s'", mysql_real_escape_string($params['name']));
		
		//description
		if(array_key_exists('description', $params)) 	
			$values[]= sprintf("`description`= '%s'", mysql_real_escape_string($params['description']));	

		$values= blg_dbschema_query_set_values($values);
	}
	
	//Rows to updated
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options)) 
			$wheres[]= sprintf("`guid`= %s", mysql_real_escape_string($options['guid']));
				
		//name
		if(array_key_exists('name', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`name`= '%s'", mysql_real_escape_string($options['name']));
			
		//description
		if(array_key_exists('description', $options))
			//TODO Change this to MATCH AGAINST 	
			$wheres[]= sprintf("`description`= '%s'", mysql_real_escape_string($options['description']));	
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->update= "`{$prefix}groups_entity`";
	$query->values= $values;
	$query->where= $where;
	return $query;	
}

/***********************************************************
 * 					   DELETE QUERIES
 ***********************************************************/

/**
 * Delete messages
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_messages($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
			
		//from_guid
		if(array_key_exists('from_guid', $options))
			$wheres[]= sprintf("`from_guid` = %s", mysql_real_escape_string($options['from_guid']));

		//to_guid
		if(array_key_exists('to_guid', $options))
			$wheres[]= sprintf("`to_guid` = %s", mysql_real_escape_string($options['to_guid']));			
			
		//subject
		if(array_key_exists('subject', $options))
			$wheres[]= sprintf("`subject` = %s", mysql_real_escape_string($options['subject']));

		//message
		if(array_key_exists('message', $options))
			//TODO Change this to MATCH AGAINST 
			$wheres[]= sprintf("`message`= '%s'", mysql_real_escape_string($options['message']));			
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options); 

		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_time_updated($options);

		//status
		if(array_key_exists('status', $options))
			$wheres[]= sprintf("`status` LIKE '%s'", mysql_real_escape_string($options['status']));

		//reply_id
		if(array_key_exists('reply_id', $options))
			$wheres[]= sprintf("`reply_id` = %s", mysql_real_escape_string(empty($options['reply_id'])?'NULL':$options['reply_id']));

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}messages`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete configuration
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_config($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();

		//param
		if(array_key_exists('param', $options))
			$wheres[]= sprintf("`param`= '%s'", mysql_real_escape_string($options['param']));			

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}config`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete files
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_files($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
			
		//url
		if(array_key_exists('url', $options))
			$wheres[]= sprintf("`url` = '%s'", mysql_real_escape_string($options['url']));

		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("`name` = '%s'", mysql_real_escape_string($options['name']));
			
		//original_name
		if(array_key_exists('original_name', $options))
			$wheres[]= sprintf("`original_name` = '%s'", mysql_real_escape_string($options['original_name']));			

		//extension
		if(array_key_exists('ext', $options))
			$wheres[]= sprintf("`ext` = '%s'", mysql_real_escape_string($options['ext']));

		//mime-type
		if(array_key_exists('mime', $options))
			$wheres[]= sprintf("`mime` = '%s'", mysql_real_escape_string($options['mime']));
			
		//owner
		if(array_key_exists('owner', $options))
			$wheres[]= sprintf("`owner` = %s", mysql_real_escape_string($options['owner']));			
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options); 

		//time_updated
		if(array_key_exists('time_updated', $options))
			$wheres[]= "`time_updated` ".blg_dbschema_query_time_updated($options);			

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}files`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete metastrings
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_metastrings($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}metastrings`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete metadata
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_metadata($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
			
		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($options['entity_guid']));

		//name_id
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("`name_id` = %s", mysql_real_escape_string($options['name_id']));

		//value_id
		if(array_key_exists('value_id', $options))
			$wheres[]= sprintf("`value_id` = %s", mysql_real_escape_string($options['value_id']));

		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($options['owner_guid']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options); 		

		//enabled
		if(array_key_exists('enabled', $options))
			$wheres[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($options['enabled']));			

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}metadata`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete annotations
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_annotations($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
			
		//entity_guid
		if(array_key_exists('entity_guid', $options))
			$wheres[]= sprintf("`entity_guid` = %s", mysql_real_escape_string($options['entity_guid']));

		//name_id
		if(array_key_exists('name_id', $options))
			$wheres[]= sprintf("`name_id` = %s", mysql_real_escape_string($options['name_id']));

		//value_id
		if(array_key_exists('value_id', $options))
			$wheres[]= sprintf("`value_id` = %s", mysql_real_escape_string($options['value_id']));

		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($options['owner_guid']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options); 				

		//enabled
		if(array_key_exists('enabled', $options))
			$wheres[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($options['enabled']));			

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}annotations`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete relationship
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_relationships($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));

		//guid
		if(array_key_exists('guid', $options)){
			$wheres[]= sprintf("( `guid_one` = %s OR `guid_two` = %s )", mysql_real_escape_string($options['guid']), mysql_real_escape_string($options['guid']));
		}
		else{			
			//guid_one
			if(array_key_exists('guid_one', $options))
				$wheres[]= sprintf("`guid_one` = %s", mysql_real_escape_string($options['guid_one']));
	
			//guid_two
			if(array_key_exists('guid_two', $options))
				$wheres[]= sprintf("`guid_two` = %s", mysql_real_escape_string($options['guid_two']));
		}
		
		//relationship
		if(array_key_exists('relationship', $options))
			$wheres[]= sprintf("`relationship` = '%s'", mysql_real_escape_string($options['relationship']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options); 				

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}entity_relationships`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete subtypes
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_subtypes($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//id
		if(array_key_exists('id', $options))
			$wheres[]= sprintf("`id` = %s", mysql_real_escape_string($options['id']));
			
		//type
		if(array_key_exists('type', $options))
			$wheres[]= sprintf("`type` = '%s'", mysql_real_escape_string($options['type']));

		//subtype
		if(array_key_exists('subtype', $options))
			$wheres[]= sprintf("`subtype` = '%s'", mysql_real_escape_string($options['subtype']));

		//class
		if(array_key_exists('class', $options))
			$wheres[]= sprintf("`class` = '%s'", mysql_real_escape_string($options['class']));

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}entity_subtypes`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete entities
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_entities($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));
			
		//type
		if(array_key_exists('type', $options))
			$wheres[]= sprintf("`type` = '%s'", mysql_real_escape_string($options['type']));

		//subtype
		if(array_key_exists('subtype', $options))
			$wheres[]= sprintf("`subtype` = %s", mysql_real_escape_string($options['subtype']));

		//owner_guid
		if(array_key_exists('owner_guid', $options))
			$wheres[]= sprintf("`owner_guid` = %s", mysql_real_escape_string($options['owner_guid']));

		//site_guid
		if(array_key_exists('site_guid', $options))
			$wheres[]= sprintf("`site_guid` = %s", mysql_real_escape_string($options['site_guid']));
			
		//container_guid
		if(array_key_exists('container_guid', $options))
			$wheres[]= sprintf("`container_guid` = %s", mysql_real_escape_string($options['container_guid']));
			
		//time_created
		if(array_key_exists('time_created', $options))
			$wheres[]= "`time_created` ".blg_dbschema_query_time_created($options);			

		//enabled
		if(array_key_exists('enabled', $options))
			$wheres[]= sprintf("`enabled` = '%s'", mysql_real_escape_string($options['enabled']));			

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}entities`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete objects
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_objects($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));
			
		//title
		if(array_key_exists('title', $options))
			$wheres[]= sprintf("`title` = '%s'", mysql_real_escape_string($options['title']));

		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}objects_entity`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete users
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_users($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));
			
		//username
		if(array_key_exists('username', $options))
			$wheres[]= sprintf("`username` = '%s'", mysql_real_escape_string($options['username']));

		//email
		if(array_key_exists('email', $options))
			$wheres[]= sprintf("`email` = '%s'", mysql_real_escape_string($options['email']));
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}users_entity`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete sites
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_sites($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));
			
		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("`name` = '%s'", mysql_real_escape_string($options['name']));

		//url
		if(array_key_exists('url', $options))
			$wheres[]= sprintf("`url` = '%s'", mysql_real_escape_string($options['url']));
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}sites_entity`";
	$query->where= $where;
	return $query;			
}

/**
 * Delete groups
 * 
 * @param $options
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_delete_groups($options=NULL){
	$prefix= blg_get_config('dbprefix');
	
	if(is_array($options)){
		$wheres= array();
		
		//guid
		if(array_key_exists('guid', $options))
			$wheres[]= sprintf("`guid` = %s", mysql_real_escape_string($options['guid']));
			
		//name
		if(array_key_exists('name', $options))
			$wheres[]= sprintf("`name` = '%s'", mysql_real_escape_string($options['name']));
			
		$where= blg_dbschema_query_where($wheres);
	}
	
	$query= new stdClass();
	$query->delete= "`{$prefix}groups_entity`";
	$query->where= $where;
	return $query;			
}


/***********************************************************
 * 					  	   COMMON
 ***********************************************************/

/**
 * Builds a string query from a stdClass query
 * @param stdClass $query
 * @return string|false Query string or false if building failed
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_buid($query, $options= NULL){
	//SELECT
	if(!empty($query->select)){
		//Count elements?
		if($options=='count'){
			$field= (empty($query->count_field)?'*':$query->count_field);
			$select= 'SELECT COUNT('.$field.') AS `result`';
		}
		else
			$select= 'SELECT '.trim($query->select);
		$from= 'FROM '.trim($query->from);
		$where= (!empty($query->where)?'WHERE '.trim($query->where):'');

		if(!empty($query->groupby)){
			$group= 'GROUP BY '.trim($query->groupby);
			if(!empty($query->having))
				$group.= ' HAVING '.trim($query->having);
		}
		
		$order= (!empty($query->order)?'ORDER BY '.trim($query->order):'');
		$limit= (!empty($query->limit)?'LIMIT '.trim($query->limit):'');
		
		$str= "$select $from $where $group $order $limit";
		return trim($str);		
	}
	
	//INSERT
	if(!empty($query->insert)){
		$insert= 'INSERT INTO '.trim($query->insert);
		$set= 'SET '.trim($query->values);
		
		$str="$insert $set";
		return trim($str);		
	}

	//UPDATE
	if(!empty($query->update)){
		$update= 'UPDATE '.trim($query->update);
		$set= 'SET '.trim($query->values);
		$where= 'WHERE '.trim($query->where);
		
		$str="$update $set $where";
		return trim($str);				
	}

	//DELETE
	if(!empty($query->delete)){
		$delete= 'DELETE FROM '.trim($query->delete);
		$where= 'WHERE '.trim($query->where);
		
		$str="$delete $where";
		return trim($str);				
	}	
}

/**
 * Builds SQL where statement
 * 
 * @param $wheres
 * @param $glue
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_where($wheres, $glue= 'AND'){
	if(is_array($wheres) && count($wheres)>0){
		$where= implode(" $glue ", $wheres);
		return $where;
	}	
	
	return '';
}

/**
 * Builds SQL values statement 
 * Example: (SET `col_name`= {expr}, `col_name`= {expr}, etc )
 * 
 * @param $values
 * @param $glue
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_set_values($values, $glue= ','){
	if(is_array($values) && count($values)>0){
		$set= implode(" $glue ", $values);
		return $set;
	}	
	
	return '';
}

/**
 * Builds SQL order statement
 * 
 * @param $options
 * @param $default_order DESC
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_order($options, $default_order= 'DESC'){
	if(array_key_exists('order', $options) && is_array($options['order']) && count($options['order'])>0){
		$order_str= '';
		foreach($options['order'] as $i=> $order){	
			$order_str.= ($i>0?', ':'');		
			// $order= array('name', TRUE);
			if(is_array($order) && count($order)==2 && is_bool($order[1])){
				$order_str.= "`{$order[0]}` ".($order[1]?'DESC':'ASC');
			}	
			// $order= array('name', 'ASC');
			elseif(is_array($order) && count($order)==2 ){
				$order_str.= "`{$order[0]}` {$order[1]}";
			}		
			// $order= array('name');			
			elseif(is_array($order) && count($order)==1){
				$order_str.= "`{$order[0]}` $default_order";
			}						
			else{
				$order_str.= "`$order` $default_order";
			}				
		}
		return $order_str;						
	}
	
	return '';
}

/**
 * Builds SQL limit statement
 * 
 * @param $options
 * @param $default_order DESC
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_limit($options){
	if(array_key_exists('limit', $options) && !empty($options['limit'])){
		if(array_key_exists('offset', $options) && !empty($options['offset']))
			$limit= "{$options['offset']}, {$options['limit']}";
		else
			$limit= "0, {$options['limit']}";
		return $limit;
	}
	
	return '';
}

/**
 * Build SQL fields
 * 
 * @param [NULL|Array] $fields
 * @param [NULL|String] $tbl_prefix
 * @return String
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_fields($fields= NULL, $tbl_prefix= NULL){
	if(empty($fields))
		return (!empty($tbl_prefix)?$tbl_prefix.'.':'').'*'; // Returns $tbl_prefix.*
		
	if(!is_array($fields))
		return (!empty($tbl_prefix)?$tbl_prefix.'.':'').'`'.$fields.'`'; // Returns $tbl_prefix.`$field`
		
	$stack= array();		
	foreach($fields as $name => $field){
		$stack[]= (!empty($tbl_prefix)?$tbl_prefix.'.':'').'`'.$field.'`'.(is_string($name)?' AS \''.$name.'\'':''); // Push $tbl_prefix.`$field`
	}
	return implode(', ', $stack);
}

/**
 * Builds time comparison condition
 * 
 * @param $options
 * @param $time_field
 * @param $default_compare
 * @return string Right side condition
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_time_field($options, $time_field, $default_compare= '>'){
	if(array_key_exists($time_field, $options)){
		//array( 0 => timestamp, 1=> (int)compare)
		if(is_array($options[$time_field]) && count($options[$time_field])==2){
			if($options[$time_field][1]===TRUE)
				$condition= "= ".mysql_real_escape_string($options[$time_field][0]); //equals
			if($options[$time_field][1]===FALSE)
				$condition= "!= ".mysql_real_escape_string($options[$time_field][0]); //not equals				
			elseif($options[$time_field][1]>0)
				$condition= "> ".mysql_real_escape_string($options[$time_field][0]); //DB great than PARAM
			elseif($options[$time_field][1]>0)
				$condition= "< ".mysql_real_escape_string($options[$time_field][0]); //DB less than PARAM		
		}
		//array( 0 => timestamp, 1=> $default_compare)
		elseif(is_array($options[$time_field]) && count($options[$time_field])==1){
			$condition= mysql_real_escape_string($default_compare)." ".mysql_real_escape_string($options[$time_field][0]);			
		}
		//string timestamp
		elseif(!is_array($options[$time_field])){
			$condition= mysql_real_escape_string($default_compare)." ".mysql_real_escape_string($options[$time_field]);
		}
		
		return $condition;
	}
	
	return '';	
}

/**
 * Builds time comparison condition on time_created
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_time_created($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'time_created', $default_comapre);	
}

/**
 * Builds time comparison condition on time_updated
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_time_updated($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'time_updated', $default_comapre);	
}

/**
 * Builds time comparison condition on last_action
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_last_action($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'last_action', $default_comapre);	
}

/**
 * Builds time comparison condition on prev_last_action
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_prev_last_action($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'prev_last_action', $default_comapre);	
}

/**
 * Builds time comparison condition on last_login
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_last_login($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'last_login', $default_comapre);	
}

/**
 * Builds time comparison condition on prev_last_login
 * @param $options
 * @param $default_comapre
 * @author Pablo
 * @since 1.0
 */
function blg_dbschema_query_prev_last_login($options, $default_comapre= '>'){
	return blg_dbschema_query_time_field($options, 'prev_last_login', $default_comapre);	
}
